package com.zhuziym.db;

import java.util.List;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;

import com.zhuziym.utils.KettleUtils;

/**
 * @Title: DemoJob.java
 * @Package com.zhuziym.db
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 作者 grq
 * @version 创建时间：2018年11月3日 上午12:35:16
 *          https://blog.csdn.net/hubeilihao/article/details/28647721
 */
public class DemoJob {
	public static void main(String[] args) throws KettleException {

		KettleEnvironment.init();
		// 创建DB资源库
		KettleDatabaseRepository repository = new KettleDatabaseRepository();
		DatabaseMeta databaseMeta = KettleUtils.setDatabaseMeta();
		// 选择资源库 此处参我与本地不一致还是正确执行
		KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta("Kettle", "Kettle",
				"Transformation description", databaseMeta);
		repository.init(kettleDatabaseRepositoryMeta);
		// 连接资源库
		repository.connect("admin", "admin");
		RepositoryDirectoryInterface directoryInterface = repository.loadRepositoryDirectoryTree();
		System.out.println(directoryInterface);// 根节点是 “/”
		List<RepositoryDirectoryInterface> children = directoryInterface.getChildren();
		for (RepositoryDirectoryInterface re : children) {
			System.out.println(re);
		}
		execJob(repository);

	}

	/**
	 * 执行 作业
	 * 
	 * @param repository
	 * @throws KettleException
	 */
	private static void execJob(KettleDatabaseRepository repository) throws KettleException {
		RepositoryDirectoryInterface dir = repository.findDirectory("/201811_JOB");
		JobMeta jobMeta = repository.loadJob(repository.getJobId("A_邮件job", dir), null);
		Job job = new Job(repository, jobMeta);
		// 设置参数
		// jobMeta.setParameterValue("method", "update");

		job.setLogLevel(LogLevel.DETAILED);
		// 启动执行指定的job
		job.start();

		job.waitUntilFinished();// 等待job执行完；
		job.setFinished(true);
		System.out.println(job.getResult());

	}

	/**
	 * 执行转换
	 * 
	 * @param repository
	 * @throws KettleException
	 */
	@SuppressWarnings("unused")
	private static void execTran(KettleDatabaseRepository repository) throws KettleException {
		// 根据指定的字符串路径 找到目录
		RepositoryDirectoryInterface dir = repository.findDirectory("/201811_KTR/A_KTR");
		// 在 指定目录下找转换
		TransMeta tmeta = repository.loadTransformation(repository.getTransformationID("A_TRAN", dir), null);
		// 设置参数
		// tmeta.setParameterValue("", "");
		Trans trans = new Trans(tmeta);
		trans.execute(null);// 执行trans
		trans.waitUntilFinished();// 等待直到数据结束
		if (trans.getErrors() > 0) {
			System.out.println("transformation error");
		} else {
			System.out.println("transformation successfully");
		}
	}

}
